<template>
	<view class="content">
		<!-- <view class="header">
			<view class="header_top">
				<text>学业成绩查询</text>
			</view>
		</view> -->
		<view class="body">
			<view class="title">
				<text>成绩查询</text>
			</view>
			<view class="title2">
				<text>Mark</text>
			</view>
			<view class="midd">
				<view class="box">

					<view class="input_1">
						<text>学号</text>
						<view class="input">
							<input name="user" type="text" confirm-type="next" @confirm="focus" v-model="form.user"
								value="" />
						</view>

					</view>

					<view class="input_2">
						<text>密码(教务系统密码)</text>
						<input name="psw" type="password" :focus="focus" v-model="form.psw" value="" />
					</view>

					<view class="input_3">

						<!-- <navigator id="txt" url="../sign/sign">未有账号？去注册...</navigator> -->

					</view>

					<view class="rud">
						<view class="rud_1">

						</view>
						<view class="rud_2">

						</view>
						<view class="clear">

						</view>
					</view>

				</view>

				<view class="login_btn">
					<text @click="submit_btn()">查询</text>
				</view>




				<uni-popup ref="popup">
					<view class="pop_root">
						<view class="text">
							<text>登录验证</text>
						</view>

						<view class="pop_img">
							<image class="ver_img" v-if="img_disp" :src="ver_img"></image>
							<image class="loading" v-else src="../../static/loading.gif" mode=""></image>



						</view>



						<view class="pop_input">
							<input type="text" v-model="ver" value="" placeholder="请输入验证码" />
						</view>


						<view class="btn">
							<button @click="cancal()" type="default">取消</button>
							<button @click="confirm()" type="default">确认</button>
						</view>
					</view>

				</uni-popup>

				<!-- <view class="any">
					<text @click="any()">直接填写</text>
				</view> -->

			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				da: 1,
				img_disp: false,
				login: false,
				this_user: 0,
				ver_img: '',
				form: {
					user: '',
					psw: ''
				},
				ver: '',
				cookies: '',
				focus: false
			}
		},

		onLoad: function() {
			var _this = this;
			uni.getStorage({
				key: "token",
				success: function(res) {
					_this.form.user = res.data.user
					_this.form.psw = res.data.psw
				}
			})
			// this.ver_img = '../../img/docu2.png'
			uni.request({
				// #ifdef MP-WEIXIN
				url: "https://pyplwf.cn/cgi-bin/search.py?status=code",
				// #endif
				
				// #ifdef H5
				url: "/cgi-bin/search.py?status=code",
				// #endif
				
				success: function(res) {
					// console.log(res.data.base64.replace(/[\r\n]/g, ""));
					console.log(res.data.cookies);
					console.log(typeof res.data.cookies);
					if (JSON.stringify(res.data.cookies) != '{}') {
						_this.ver_img = res.data.base64
						_this.cookies = JSON.stringify(res.data.cookies)
						_this.img_disp = true

					} else {
						uni.showToast({
							icon: 'none',
							title: '拉取登录数据失败'
						});
					}

				},
				fail: function() {



				}
			})
		},
		methods: {
			pag: function(event) {
				uni.navigateTo({
					url: 'pages/index'
				})
			},
			// 查询按钮
			submit_btn() {
				var _this = this
				if (this.form.user == '' || this.form.psw == '') {
					uni.showToast({
						title: "请输入账号密码",
						icon: "none"
					});
				} else {
					//若已登录直接请求成绩列表
					if (this.login != false) {
						uni.showLoading({
							title: '正在请求'
						})


						if (this.this_user != this.form.user) {
							//输入框学号变化
							uni.request({
								// #ifdef MP-WEIXIN
								url: "https://pyplwf.cn/cgi-bin/search.py?status=code",
								// #endif
								
								// #ifdef H5
								url: "/cgi-bin/search.py?status=code",
								// #endif
								
								success: function(res) {
									// console.log(res.data.base64.replace(/[\r\n]/g, ""));
									_this.ver_img = res.data.base64
									_this.cookies = JSON.stringify(res.data.cookies)
									_this.img_disp = true;
									_this.user = _this.form.user;
									uni.hideLoading();
									_this.$refs.popup.open();

								}
							});
						} else {
							uni.request({
								// #ifdef MP-WEIXIN
								url: "https://pyplwf.cn/cgi-bin/search.py?status=logon&cookies=" + _this.cookies,
								// #endif
								
								// #ifdef H5
								url: "/cgi-bin/search.py?status=logon&cookies=" + _this.cookies,
								// #endif
								
								success: function(res) {

									uni.setStorage({
										key: 'marks',
										data: res.data
									})

									uni.hideLoading();
									uni.showToast({
										title: "查询成功",
										icon: "none"
									});
									uni.navigateTo({
										url: "./info/info"
									});
								}
							});
						}
					} else {
						this.$refs.popup.open();
					}
				}

			},

			// 按下对话框确认键
			confirm: function() {
				var _this = this
				// 判断验证码是否为空
				if (this.ver == '') {
					uni.showToast({
						title: "请输入验证码",
						icon: "none"
					});
				} else {
					uni.showLoading({
						title: '查询中'
					});
					// 登录
					uni.request({
						// #ifdef MP-WEIXIN
						url: 'https://pyplwf.cn/cgi-bin/search.py?status=login&user=' + _this.form.user + '&psw=' + _this
							.form.psw + '&cookies=' + _this.cookies + '&code=' + _this.ver,
						// #endif
						
						// #ifdef H5
						url: '/cgi-bin/search.py?status=login&user=' + _this.form.user + '&psw=' + _this
							.form.psw + '&cookies=' + _this.cookies + '&code=' + _this.ver,
						// #endif
						
						success: function(res) {
							// 登录成功执行代码
							if (res.data.code == 200) {
								console.log('登录成功')
								_this.login = true;
								_this.this_user = _this.form.user;
								uni.setStorage({
									key: 'token',
									data: {
										user: _this.form.user,
										psw: _this.form.psw
									},
								});


								// 请求成绩数据
								uni.request({
									// #ifdef MP-WEIXIN
									url: "https://pyplwf.cn/cgi-bin/search.py?status=logon&cookies=" + _this
										.cookies,
									// #endif
									
									// #ifdef H5
									url: "/cgi-bin/search.py?status=logon&cookies=" + _this
										.cookies,
									// #endif
									
									success: function(res) {

										// 请求成功赋值全局变量
										uni.setStorage({
											key: 'marks',
											data: res.data
										})
										_this.ver = '';
										// uni.hideLoading();
										uni.showToast({
											title: "查询成功",
											icon: "none"
										});

										_this.$refs.popup.close()
										// 跳转成绩展示页面
										uni.navigateTo({
											url: "./info/info"
										});
									}
								});
								// 登录失败执行代码
							} else {
								uni.hideLoading();
								uni.showToast({
									title: "错误",
									icon: "none"
								});

								//清空密码输入框与验证码输入框
								_this.form.psw = '';
								_this.ver = '';

								_this.img_disp = false;
								uni.request({
									// #ifdef MP-WEIXIN
									url: "https://pyplwf.cn/cgi-bin/search.py?status=code",
									// #endif
									// #ifdef H5
									url: "/cgi-bin/search.py?status=code",
									// #endif
									success: function(res) {
										// console.log(res.data.base64.replace(/[\r\n]/g, ""));
										_this.ver_img = res.data.base64
										_this.cookies = JSON.stringify(res.data.cookies)
										_this.img_disp = true;
									}
								});
							};
						}
					})


				}
			},
			cancal: function() {

				this.$refs.popup.close();
			},

			any() {
				uni.navigateTo({
					url: '../chunjie/chunjie'
				})
			},
			done: function() {
				this.focus = true
			},
		}
	}
</script>

<style>
	@import url("mark.css");
</style>
